package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.constant.CommonConstant;
import com.parking.mgr.common.exception.ServiceException;
import com.parking.mgr.common.exception.enums.CommonExceptionEnum;
import com.parking.mgr.common.factory.PageFactory;
import com.parking.mgr.common.util.IdUtils;
import com.parking.mgr.useraccount.model.entity.ParkingRecordInfoEntity;
import com.parking.mgr.useraccount.model.mapper.ParkingRecordInfoMapper;
import com.parking.mgr.useraccount.model.mapstruct.ParkingRecordInfoMapStruct;
import com.parking.mgr.useraccount.model.param.ParkingRecordInfoParam;
import com.parking.mgr.useraccount.model.vo.ParkingRecordInfoVO;
import com.parking.mgr.useraccount.service.LoginTokenService;
import com.parking.mgr.useraccount.service.ParkingRecordInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ParkingRecordInfoServiceImpl
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/3/6
 */
@Service
@Slf4j
public class ParkingRecordInfoServiceImpl extends ServiceImpl<ParkingRecordInfoMapper, ParkingRecordInfoEntity> implements ParkingRecordInfoService {

    @Resource
    private ParkingRecordInfoMapStruct parkingRecordInfoMapStruct;

    @Resource
    private LoginTokenService loginTokenService;

    @Override
    public String addParkingRecordInfo(ParkingRecordInfoParam parkingRecordInfoParam, HttpServletRequest request) {

        String userPassportId = loginTokenService.queryPassportByClientIdToken(request.getHeader(CommonConstant.LOGIN_TOKEN), request.getHeader(CommonConstant.CLIENT_ID));


        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoMapStruct.param2Entity(parkingRecordInfoParam);

        if (StrUtil.isBlank(parkingRecordInfoParam.getId())) {
            parkingRecordInfoEntity.setId(IdUtils.getId());
            parkingRecordInfoEntity.setFkUserPassportId(userPassportId);
            parkingRecordInfoEntity.setFReviewStatus(CommonConstant.FILLING);
            this.save(parkingRecordInfoEntity);
        } else {
            String id = parkingRecordInfoParam.getId();
            parkingRecordInfoEntity.setId(id);
            parkingRecordInfoEntity.setFkUserPassportId(userPassportId);
            this.updateById(parkingRecordInfoEntity);
        }

        return parkingRecordInfoEntity.getId();
    }

    @Override
    public void updateParkingRecordInfo(ParkingRecordInfoParam parkingRecordInfoParam, HttpServletRequest request) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoMapStruct.param2Entity(parkingRecordInfoParam);

        this.updateById(parkingRecordInfoEntity);
    }

    @Override
    public IPage<ParkingRecordInfoVO> list(ParkingRecordInfoParam parkingRecordInfoParam, HttpServletRequest request) {

        String userPassportId = loginTokenService.queryPassportByClientIdToken(request.getHeader(CommonConstant.LOGIN_TOKEN), request.getHeader(CommonConstant.CLIENT_ID));

        LambdaQueryWrapper<ParkingRecordInfoEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordInfoEntity>()
                .eq(ParkingRecordInfoEntity::getFkUserPassportId, userPassportId);

        Page<ParkingRecordInfoEntity> page = PageFactory.defaultPage(parkingRecordInfoParam.getPageSize(), parkingRecordInfoParam.getCurrPage());

        return parkingRecordInfoMapStruct.entity2VO(this.page(page, queryWrapper));
    }

    @Override
    public ParkingRecordInfoVO detail(String id) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = this.getById(id);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException(CommonExceptionEnum.DATA_NOT_EXIST);
        }

        return parkingRecordInfoMapStruct.entity2VO(parkingRecordInfoEntity);
    }

}
